CREATE PROCEDURE [acms].[EmailActivationCode_Put]
			@Id int,
            @UserId int,
            @Code VarChar(10),
            @EmailAddress VarChar(255),
            @Activated DateTime,
            @Created DateTime,
            @ConcurencyId rowversion,
            @IdOut Int output,
            @ConcurencyIdOut Rowversion output

AS
	if(@Id = 0) begin
		insert into EmailActivationCode
			(UserId, Code, EmailAddress, Activated, Created)
		values
			(@UserId, @Code, @EmailAddress, @Activated, @Created)
		set @IdOut = @@Identity
	end
	else if exists (select null from EmailActivationCode Where Id = @Id and ConcurencyId = @ConcurencyId and Deleted = 0) begin
		update EmailActivationCode
		set
			UserId = @UserId, 
			Code = @Code, 
			EmailAddress = @EmailAddress, 
			Activated = @Activated
		where @Id = @Id
		set @IdOut = @Id
	end
	else if exists (select null from EmailActivationCode Where Id = @Id and Deleted = 0) begin
		return -100
	end
	else begin
		return -200
	end
	
	select @ConcurencyIdOut = ConcurencyId from EmailActivationCode where Id = @IdOut
RETURN 0